<?php

namespace App\Http\Controllers;

use App\Service\BMR\BMR_Metabolize;
use App\Service\BMR\Metabolize\BasialMetabolize;
use App\Service\BMR\Metabolize\Man;
use App\Service\BMR\Metabolize\Woman;
use App\Service\BMR\Nutrition\Carbohydrate;
use App\Service\BMR\Nutrition\Fat;
use App\Service\BMR\Nutrition\Protein;
use App\Service\UserBuilderService;
use App\Service\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CalculateController extends Controller
{
    //评测表单页
    public function index() {
        $data = [
            'user' => $this->bmrUser,
            'info' => $this->bmrUser->lastBuilder()->first()
        ];
        return view('bmr/calculate', $data);
    }

    //评测
    public function update(Request $request, UserService $userService, UserBuilderService $userBuilderService) {
        if ($request->isMethod('post')) {
            $inputs = $request->all();

            DB::transaction(function() use ($userService, $userBuilderService, $inputs) {
                //统计食物营养以及热量
                $inputs = array_merge($inputs, $userBuilderService->getMbr($inputs));
                //更新用户性别
                $userService->updateSex($this->bmrUser, $inputs['sex']);
                //更新热量数据
                $userBuilderService->updateOrCreate($this->bmrUser, $inputs);
            });
            $data = [];
            return redirect('/date');
        }
    }
}